#!/usr/bin/perl -w
use Weather::Com;
use Weather::Com::Finder;
use Data::Dumper;

# you have to fill in your ids from weather.com here
sub buscar {
  my $busqueda = shift;
  my $provincia = "@_";
  my $PartnerId  = '1054529275';
  my $LicenseKey = '15247d6a6e9a86dd';

  my %weatherargs = (
        'partner_id' => $PartnerId,
        'license'    => $LicenseKey,
        'language'   => 'en',
  );
 
  my $weather_finder = Weather::Com::Finder->new(%weatherargs);
  
  my $locations = $weather_finder->find($busqueda);
  if (!defined($locations)) {
       
	if (defined($provincia)) {
               
		$locations = $weather_finder->find($provincia);	
                
	} else {
		return undef;
	}
 }
 my @localizaciones = @{$locations};

  foreach my $location (@localizaciones) {
    if ($location->name() =~ /Spain/i) {
    	my $icono = $location->current_conditions()->icon();
        my $forecast = $location->forecast();
        my $tomorrow = $forecast->day(1)->day()->icon();
        my $pasado = $forecast->day(2)->day()->icon();
        
	my %salida = (ID => $location->id(), HOY => $icono, MANA => $tomorrow, PM => $pasado);
        ##print $salida{ID}, "\n";
	return \%salida;
    }
   
  }
  return undef; # No hay una localización española
}
1;
#buscar ("@ARGV");
#buscar ("Tenerife", "Candelaria");
